<?php
class KetquasController extends AppController {

	var $name = 'Ketquas';
	var $helpers = array('Html', 'Form');
	var $uses = array('Canbo', 'User', 'Trambvtv', 'PhieuDt','NdDt','Doituong','LoaiDt','Huyen','Tinh','KqBd','PDtBd','Nam','Bayden');
		//doan code de xet quyen
	function beforeFilter(){
        parent::beforeFilter();
        $curUser = $this->Auth->user();
        if($curUser['User']['ROLE']!='admin'&&$curUser['User']['ROLE']!='user'){
            $this->Session->setFlash('Bạn phải đăng nhập bằng tài khoản Admin để sử dụng chức năng này');
            $this->redirect('/pages');
        }
    }
	
	function index1() {
		$this->Ketqua->recursive = 0;
		$this->set('ketquas', $this->paginate());
	}

	function tinhs() {
			$this->set('namsa', $this->Nam->findAll());
			$this->set('doituongsa', $this->Doituong->findAll());
			$this->set('tinhsa', $this->Tinh->findAll());

			$this->set('thangs',$this->data['tinhs']['THANG']);
			$this->set('nams',$this->Nam->findById($this->data['tinhs']['NAM']));
			
			switch ($this->data['tinhs']['THANG']){
			case 1;
			case 3;
			case 5;
			case 7;
			case 8;
			case 10;
			case 12;
				$songay = 31;
				break;
			case 2;
				if (($this->data['tinhs']['NAM'] % 4) == 0) $songay = 29; else $songay = 28; 
				break;
			default:	
				$songay = 30;
				break;
			}
			
			$this->set('songay',$songay);
			$this->set('tinhs', $this->Tinh->findById($this->data['tinhs']['TINH']));
			$this->set('huyens', $this->Huyen->findAllBytinh_id($this->data['tinhs']['TINH']));
			$this->set('doituongs', $this->Doituong->findByid($this->data['tinhs']['DOITUONG']));

			$shuyen = $this->Huyen->findAllBytinh_id($this->data['tinhs']['TINH']) ;
			$x = 1;
			foreach ($shuyen as $huyen):
			
			$datay = $this->Bayden->find('list',
	       	array('conditions'=>array('Trambvtv.huyen_id'=> $huyen['Huyen']['id'] ),'recursive' => 0,'contain' => array('Trambvtv'),
									'fields' => array('Bayden.id')
			) );

			$bayden_id = "(";
			foreach ($datay as $dataxx):
			$i = 0;
			$bayden_id = $bayden_id.$dataxx.',';
			$i++;
			endforeach;	
			$bayden_id = $bayden_id.'0)';
			$hid = $huyen['Huyen']['id'];
			$datas["$hid"] = ($this->KqBd->find('all',
	       	array('conditions'=>array('PDtBd.bayden_id IN ' . $bayden_id,
									  'KqBd.doituong_id'=> $this->data['tinhs']['DOITUONG'],
									  'YEAR(PDtBd.NGAY_PBD)' => $this->data['tinhs']['NAM'],
									  'MONTH(PDtBd.NGAY_PBD)' => $this->data['tinhs']['THANG'],
  									  ),'recursive' => 0,'contain' => array('PDtBd'),'group' => 'PDtBd.NGAY_PBD',
				  					  'fields' => array('DAY(PDtBd.NGAY_PBD) as NGAY','SUM(KqBd.SL_TREN_BD) as KQ'),
				  						
				  )) );
			$x++;
			endforeach;
			$this->set ('datas',$datas);
	}




	function dthuyens() {
			$this->set('namsa', $this->Nam->findAll());
			$this->set('doituongsa', $this->Doituong->findAll());
			$this->set('huyensa', $this->Huyen->findAll());

			$this->set('thangs',$this->data['dthuyens']['THANG']);
			$this->set('nams',$this->Nam->findById($this->data['dthuyens']['NAM']));
			$this->set('huyens',$this->Huyen->findById($this->data['dthuyens']['HUYEN']));
	
			switch ($this->data['dthuyens']['THANG']){
			case 1;
			case 3;
			case 5;
			case 7;
			case 8;
			case 10;
			case 12;
				$songay = 31;
				break;
			case 2;
				if (($this->data['dthuyens']['NAM'] % 4) == 0) $songay = 29; else $songay = 28; 
				break;
			default:	
				$songay = 30;
				break;
			}
			
			$this->set('songay',$songay);
			//$this->set('tinhs', $this->Tinh->findById($this->data['dthuyens']['TINH']));
			//$this->set('huyens', $this->Huyen->findAllByid($this->data['dthuyens']['HUYEN']));
			

			$sdoituong = $this->Doituong->find('all',array('recursive' => 0,
														'fields' => array('id','TEN_DT'),
														'group' => 'Doituong.id'));
			$this->set('doituongs', $sdoituong);
			$x = 0;
			foreach ($sdoituong as $doituongx):
			
			$datay = $this->Bayden->find('list',
	       	array('conditions'=>array('Trambvtv.huyen_id'=> $this->data['dthuyens']['HUYEN'] ),'recursive' => 0,'contain' => array('Trambvtv'),
									'fields' => array('Bayden.id')
			) );

			$bayden_id = "(";
			foreach ($datay as $dataxx):
			$bayden_id = $bayden_id.$dataxx.',';
			endforeach;	
			$bayden_id = $bayden_id.'0)';
			$dtid = $doituongx['Doituong']['id'];
			$datas["$dtid"] = ($this->KqBd->find('all',
	       	array('conditions'=>array('PDtBd.bayden_id IN ' . $bayden_id,
									  'KqBd.doituong_id'=> $doituongx['Doituong']['id'],
									  'YEAR(PDtBd.NGAY_PBD)' => $this->data['dthuyens']['NAM'],
									  'MONTH(PDtBd.NGAY_PBD)' => $this->data['dthuyens']['THANG'],
  									  ),'recursive' => 0,'contain' => array('PDtBd'),'group' => 'PDtBd.NGAY_PBD',
				  					  'fields' => array('DAY(PDtBd.NGAY_PBD) as NGAY','SUM(KqBd.SL_TREN_BD) as KQ'),
				  						
				  )) );
			$x++;
			endforeach;
			$this->set ('datas',$datas);
	}


	function dttinhs() {
			$this->set('namsa', $this->Nam->findAll());
			$this->set('tinhsa', $this->Tinh->findAll());

			$this->set('thangs',$this->data['dttinhs']['THANG']);
			$this->set('nams',$this->Nam->findById($this->data['dttinhs']['NAM']));
			
			switch ($this->data['dttinhs']['THANG']){
			case 1;
			case 3;
			case 5;
			case 7;
			case 8;
			case 10;
			case 12;
				$songay = 31;
				break;
			case 2;
				if (($this->data['dttinhs']['NAM'] % 4) == 0) $songay = 29; else $songay = 28; 
				break;
			default:	
				$songay = 30;
				break;
			}
			
			$this->set('songay',$songay);
			//$this->set('tinhs', $this->Tinh->findById($this->data['dthuyens']['TINH']));
			//$this->set('huyens', $this->Huyen->findAllByid($this->data['dthuyens']['HUYEN']));
			$this->set('tinhs', $this->Tinh->findById($this->data['dttinhs']['TINH']));
			$huyens = $this->Huyen->find('list',array('conditions'=>array('Huyen.tinh_id'=> $this->data['dttinhs']['TINH']),'recursive' => 0,'fields' => array('Huyen.id') ));
			
			$huyens_id = "(";
			foreach ($huyens as $datayy):
			$huyens_id = $huyens_id.$datayy.',';
			endforeach;	
			$huyens_id = $huyens_id.'0)';
			
			$sdoituong = $this->Doituong->find('all',array('recursive' => 0,
														'fields' => array('id','TEN_DT'),
														'group' => 'Doituong.id'));
			$this->set('doituongs', $sdoituong);
			$x = 0;
			foreach ($sdoituong as $doituongy):

			$datay = $this->Bayden->find('list',
	       	array('conditions'=>array('Trambvtv.huyen_id IN ' . $huyens_id ),'recursive' => 0,'contain' => array('Trambvtv'),
									'fields' => array('Bayden.id')
			) );

			$bayden_id = "(";
			foreach ($datay as $dataxx):
			$bayden_id = $bayden_id.$dataxx.',';
			endforeach;	
			$bayden_id = $bayden_id.'0)';
			$dtid = $doituongy['Doituong']['id'];
			$datas["$dtid"] = ($this->KqBd->find('all',
	       	array('conditions'=>array('PDtBd.bayden_id IN ' . $bayden_id,
									  'KqBd.doituong_id'=> $doituongy['Doituong']['id'],
									  'YEAR(PDtBd.NGAY_PBD)' => $this->data['dttinhs']['NAM'],
									  'MONTH(PDtBd.NGAY_PBD)' => $this->data['dttinhs']['THANG'],
  									  ),'recursive' => 0,'contain' => array('PDtBd'),'group' => 'PDtBd.NGAY_PBD',
				  					  'fields' => array('DAY(PDtBd.NGAY_PBD) as NGAY','SUM(KqBd.SL_TREN_BD) as KQ'),
				  						
				  )) ) ;
			$x++;
			endforeach;
			$this->set ('datas',$datas);
	}



	function dts() {
			$this->set('namsa', $this->Nam->findAll());
			$this->set('doituongsa', $this->Doituong->findAll());

			$this->set('thangs',$this->data['dts']['THANG']);
			$this->set('nams',$this->Nam->findById($this->data['dts']['NAM']));
		
			switch ($this->data['dts']['THANG']){
			case 1;
			case 3;
			case 5;
			case 7;
			case 8;
			case 10;
			case 12;
				$songay = 31;
				break;
			case 2;
				if (($this->data['dts']['NAM'] % 4) == 0) $songay = 29; else $songay = 28; 
				break;
			default:	
				$songay = 30;
				break;
			}
			
			$this->set ('tdoituong',$this->Doituong->findById($this->data['dts']['DOITUONG']));
			$this->set('songay',$songay);
			$huyens = $this->Huyen->find('all',array('recursive' => 0,
														'fields' => array('id','TEN_H'),
														'group' => 'Huyen.id'));
			$this->set('huyens', $huyens);


			$tinhs = $this->Tinh->find('all',array('recursive' => 0,
														'fields' => array('id','TEN_T'),
														'group' => 'Tinh.id'));
			$this->set('tinhs', $tinhs);
			
			
			$sdoituong = $this->Doituong->find('all',array('recursive' => 0,
														'fields' => array('id','TEN_DT'),
														'group' => 'Doituong.id'));
			$this->set('doituongs', $sdoituong);

			// LIET KE THEO TINH
			if ($this->data['dts']['TYPE'] == 1 ){
				foreach ($tinhs as $tinh):
				
				$huyens = $this->Huyen->find('list',array('conditions'=>array('Huyen.tinh_id' => $tinh['Tinh']['id']),'recursive' => 0,'fields' => array('Huyen.id') ));
				$huyens_id = "(";
				foreach ($huyens as $dataxx):
				$huyens_id = $huyens_id.$dataxx.',';
				endforeach;	
				$huyens_id = $huyens_id.'0)';
				$x = 0;
				$datay = $this->Bayden->find('list',
				array('conditions'=>array('Trambvtv.huyen_id IN ' . $huyens_id ),'recursive' => 0,'contain' => array('Trambvtv'),
										'fields' => array('Bayden.id')
				) );
	
				$bayden_id = "(";
				foreach ($datay as $dataxx):
				$i = 0;
				$bayden_id = $bayden_id.$dataxx.',';
				$i++;
				endforeach;	
				
				$bayden_id = $bayden_id.'0)';
				$tinh_id = $tinh['Tinh']['id'];
				$datas["$tinh_id"] = ($this->KqBd->find('all',
				array('conditions'=>array('PDtBd.bayden_id IN ' . $bayden_id,
										  'KqBd.doituong_id'=> $this->data['dts']['DOITUONG'],
										  'YEAR(PDtBd.NGAY_PBD)' => $this->data['dts']['NAM'],
										  'MONTH(PDtBd.NGAY_PBD)' => $this->data['dts']['THANG'],
										  ),'recursive' => 0,'contain' => array('PDtBd'),'group' => 'PDtBd.NGAY_PBD',
										  'fields' => array('DAY(PDtBd.NGAY_PBD) as NGAY','SUM(KqBd.SL_TREN_BD) as KQ'),
											
					  )) );
				$x++;
				endforeach;
				$this->set ('datas',$datas);
			}
			// LIET KE THEO HUYEN
			else if ($this->data['dts']['TYPE'] == 2 ){
				foreach ($huyens as $huyen):
				$x = 0;
				$datay = $this->Bayden->find('list',
				array('conditions'=>array('Trambvtv.huyen_id' => $huyen['Huyen']['id'] ),'recursive' => 0,'contain' => array('Trambvtv'),
										'fields' => array('Bayden.id')
				) );
	
				$bayden_id = "(";
				foreach ($datay as $dataxx):
				$i = 0;
				$bayden_id = $bayden_id.$dataxx.',';
				$i++;
				endforeach;	
				$bayden_id = $bayden_id.'0)';
				$huyen_id = $huyen['Huyen']['id'];
				$datas[$huyen_id] = $this->KqBd->find('all',
				array('conditions'=>array('PDtBd.bayden_id IN ' . $bayden_id,
										  'KqBd.doituong_id'=> $this->data['dts']['DOITUONG'],
										  'YEAR(PDtBd.NGAY_PBD)' => $this->data['dts']['NAM'],
										  'MONTH(PDtBd.NGAY_PBD)' => $this->data['dts']['THANG'],
										  ),'recursive' => 0,'contain' => array('PDtBd'),'group' => 'PDtBd.NGAY_PBD',
										  'fields' => array('DAY(PDtBd.NGAY_PBD) as NGAY','SUM(KqBd.SL_TREN_BD) as KQ'),
											
					  )) ;
				$x++;
				$this->set ('datas',$datas);
				endforeach;
				
			}
	}
	
	function tinhs_xls() {
			$tinhs_xls = $this->params['named'];
			$this->set ('tinhs_xls',$tinhs_xls);
			$this->set('thangs',$tinhs_xls['THANG']);
			$this->set('nams',$this->Nam->findById($tinhs_xls['NAM']));
			
			switch ($tinhs_xls['THANG']){
			case 1;
			case 3;
			case 5;
			case 7;
			case 8;
			case 10;
			case 12;
				$songay = 31;
				break;
			case 2;
				if (($tinhs_xls['NAM'] % 4) == 0) $songay = 29; else $songay = 28; 
				break;
			default:	
				$songay = 30;
				break;
			}
			
			$this->set('songay',$songay);
			$this->set('tinhs', $this->Tinh->findById($tinhs_xls['TINH']));
			$this->set('huyens', $this->Huyen->findAllBytinh_id($tinhs_xls['TINH']));
			$this->set('doituongs', $this->Doituong->findByid($tinhs_xls['DOITUONG']));

			$shuyen = $this->Huyen->findAllBytinh_id($tinhs_xls['TINH']) ;
			$x = 1;
			foreach ($shuyen as $huyen):
			
			$datay = $this->Bayden->find('list',
	       	array('conditions'=>array('Trambvtv.huyen_id'=> $huyen['Huyen']['id'] ),'recursive' => 0,'contain' => array('Trambvtv'),
									'fields' => array('Bayden.id')
			) );

			$bayden_id = "(";
			foreach ($datay as $dataxx):
			$i = 0;
			$bayden_id = $bayden_id.$dataxx.',';
			$i++;
			endforeach;	
			$bayden_id = $bayden_id.'0)';
			$hid = $huyen['Huyen']['id'];
			$datas["$hid"] = ($this->KqBd->find('all',
	       	array('conditions'=>array('PDtBd.bayden_id IN ' . $bayden_id,
									  'KqBd.doituong_id'=> $tinhs_xls['DOITUONG'],
									  'YEAR(PDtBd.NGAY_PBD)' => $tinhs_xls['NAM'],
									  'MONTH(PDtBd.NGAY_PBD)' => $tinhs_xls['THANG'],
  									  ),'recursive' => 0,'contain' => array('PDtBd'),'group' => 'PDtBd.NGAY_PBD',
				  					  'fields' => array('DAY(PDtBd.NGAY_PBD) as NGAY','SUM(KqBd.SL_TREN_BD) as KQ'),
				  						
				  )) );
			$x++;
			endforeach;
			$this->set ('datas',$datas);
			$this->layout = '';
	}



	function dthuyens_xls() {
			$dthuyens_xls = $this->params['named'];
			$this->set('dthuyens_xls',$dthuyens_xls);

			$this->set('thangs',$dthuyens_xls['THANG']);
			$this->set('nams',$this->Nam->findById($dthuyens_xls['NAM']));
			$this->set('huyens',$this->Huyen->findById($dthuyens_xls['HUYEN']));
	
			switch ($dthuyens_xls['THANG']){
			case 1;
			case 3;
			case 5;
			case 7;
			case 8;
			case 10;
			case 12;
				$songay = 31;
				break;
			case 2;
				if (($dthuyens_xls['NAM'] % 4) == 0) $songay = 29; else $songay = 28; 
				break;
			default:	
				$songay = 30;
				break;
			}
			
			$this->set('songay',$songay);

			$sdoituong = $this->Doituong->find('all',array('recursive' => 0,
														'fields' => array('id','TEN_DT'),
														'group' => 'Doituong.id'));
			$this->set('doituongs', $sdoituong);
			$x = 0;
			foreach ($sdoituong as $doituongx):
			
			$datay = $this->Bayden->find('list',
	       	array('conditions'=>array('Trambvtv.huyen_id'=> $dthuyens_xls['HUYEN'] ),'recursive' => 0,'contain' => array('Trambvtv'),
									'fields' => array('Bayden.id')
			) );

			$bayden_id = "(";
			foreach ($datay as $dataxx):
			$bayden_id = $bayden_id.$dataxx.',';
			endforeach;	
			$bayden_id = $bayden_id.'0)';
			$dtid = $doituongx['Doituong']['id'];
			$datas["$dtid"] = ($this->KqBd->find('all',
	       	array('conditions'=>array('PDtBd.bayden_id IN ' . $bayden_id,
									  'KqBd.doituong_id'=> $doituongx['Doituong']['id'],
									  'YEAR(PDtBd.NGAY_PBD)' => $dthuyens_xls['NAM'],
									  'MONTH(PDtBd.NGAY_PBD)' => $dthuyens_xls['THANG'],
  									  ),'recursive' => 0,'contain' => array('PDtBd'),'group' => 'PDtBd.NGAY_PBD',
				  					  'fields' => array('DAY(PDtBd.NGAY_PBD) as NGAY','SUM(KqBd.SL_TREN_BD) as KQ'),
				  						
				  )) );
			$x++;
			endforeach;
			$this->set ('datas',$datas);
			$this->layout = '';
	}
	
	

	function dttinhs_xls() {
			$dttinhs_xls = $this->params['named'];
			$this->set('dttinhs_xls',$dttinhs_xls);
			
			$this->set('thangs',$dttinhs_xls['THANG']);
			$this->set('nams',$this->Nam->findById($dttinhs_xls['NAM']));
			
			switch ($dttinhs_xls['THANG']){
			case 1;
			case 3;
			case 5;
			case 7;
			case 8;
			case 10;
			case 12;
				$songay = 31;
				break;
			case 2;
				if (($dttinhs_xls['NAM'] % 4) == 0) $songay = 29; else $songay = 28; 
				break;
			default:	
				$songay = 30;
				break;
			}
			
			$this->set('songay',$songay);

$this->set('tinhs', $this->Tinh->findById($dttinhs_xls['TINH']));
			$huyens = $this->Huyen->find('list',array('conditions'=>array('Huyen.tinh_id'=> $dttinhs_xls['TINH']),'recursive' => 0,'fields' => array('Huyen.id') ));
			
			$huyens_id = "(";
			foreach ($huyens as $datayy):
			$huyens_id = $huyens_id.$datayy.',';
			endforeach;	
			$huyens_id = $huyens_id.'0)';
			
			$sdoituong = $this->Doituong->find('all',array('recursive' => 0,
														'fields' => array('id','TEN_DT'),
														'group' => 'Doituong.id'));
			$this->set('doituongs', $sdoituong);
			$x = 0;
			foreach ($sdoituong as $doituongy):

			$datay = $this->Bayden->find('list',
	       	array('conditions'=>array('Trambvtv.huyen_id IN ' . $huyens_id ),'recursive' => 0,'contain' => array('Trambvtv'),
									'fields' => array('Bayden.id')
			) );

			$bayden_id = "(";
			foreach ($datay as $dataxx):
			$bayden_id = $bayden_id.$dataxx.',';
			endforeach;	
			$bayden_id = $bayden_id.'0)';
			$dtid = $doituongy['Doituong']['id'];
			$datas["$dtid"] = ($this->KqBd->find('all',
	       	array('conditions'=>array('PDtBd.bayden_id IN ' . $bayden_id,
									  'KqBd.doituong_id'=> $doituongy['Doituong']['id'],
									  'YEAR(PDtBd.NGAY_PBD)' => $dttinhs_xls['NAM'],
									  'MONTH(PDtBd.NGAY_PBD)' => $dttinhs_xls['THANG'],
  									  ),'recursive' => 0,'contain' => array('PDtBd'),'group' => 'PDtBd.NGAY_PBD',
				  					  'fields' => array('DAY(PDtBd.NGAY_PBD) as NGAY','SUM(KqBd.SL_TREN_BD) as KQ'),
				  						
				  )) ) ;
			$x++;
			endforeach;
			$this->set ('datas',$datas);
			$this->layout = '';
	}





	function dts_xls() {
			$dts_xls = $this->params['named'];
			$this->set('dts_xls',$dts_xls);

			$this->set('thangs',$dts_xls['THANG']);
			$this->set('nams',$this->Nam->findById($dts_xls['NAM']));
		
			switch ($dts_xls['THANG']){
			case 1;
			case 3;
			case 5;
			case 7;
			case 8;
			case 10;
			case 12;
				$songay = 31;
				break;
			case 2;
				if (($dts_xls['NAM'] % 4) == 0) $songay = 29; else $songay = 28; 
				break;
			default:	
				$songay = 30;
				break;
			}
			
			$this->set ('tdoituong',$this->Doituong->findById($dts_xls['DOITUONG']));
			$this->set('songay',$songay);
			$huyens = $this->Huyen->find('all',array('recursive' => 0,
														'fields' => array('id','TEN_H'),
														'group' => 'Huyen.id'));
			$this->set('huyens', $huyens);


			$tinhs = $this->Tinh->find('all',array('recursive' => 0,
														'fields' => array('id','TEN_T'),
														'group' => 'Tinh.id'));
			$this->set('tinhs', $tinhs);
			
			
			$sdoituong = $this->Doituong->find('all',array('recursive' => 0,
														'fields' => array('id','TEN_DT'),
														'group' => 'Doituong.id'));
			$this->set('doituongs', $sdoituong);

			// LIET KE THEO TINH
			if ($dts_xls['TYPE'] == 1 ){
				foreach ($tinhs as $tinh):
				
				$huyens = $this->Huyen->find('list',array('conditions'=>array('Huyen.tinh_id' => $tinh['Tinh']['id']),'recursive' => 0,'fields' => array('Huyen.id') ));
				$huyens_id = "(";
				foreach ($huyens as $dataxx):
				$huyens_id = $huyens_id.$dataxx.',';
				endforeach;	
				$huyens_id = $huyens_id.'0)';
				$x = 0;
				$datay = $this->Bayden->find('list',
				array('conditions'=>array('Trambvtv.huyen_id IN ' . $huyens_id ),'recursive' => 0,'contain' => array('Trambvtv'),
										'fields' => array('Bayden.id')
				) );
	
				$bayden_id = "(";
				foreach ($datay as $dataxx):
				$i = 0;
				$bayden_id = $bayden_id.$dataxx.',';
				$i++;
				endforeach;	
				
				$bayden_id = $bayden_id.'0)';
				$tinh_id = $tinh['Tinh']['id'];
				$datas["$tinh_id"] = ($this->KqBd->find('all',
				array('conditions'=>array('PDtBd.bayden_id IN ' . $bayden_id,
										  'KqBd.doituong_id'=> $dts_xls['DOITUONG'],
										  'YEAR(PDtBd.NGAY_PBD)' => $dts_xls['NAM'],
										  'MONTH(PDtBd.NGAY_PBD)' => $dts_xls['THANG'],
										  ),'recursive' => 0,'contain' => array('PDtBd'),'group' => 'PDtBd.NGAY_PBD',
										  'fields' => array('DAY(PDtBd.NGAY_PBD) as NGAY','SUM(KqBd.SL_TREN_BD) as KQ'),
											
					  )) );
				$x++;
				endforeach;
				$this->set ('datas',$datas);
				$this->layout = '';
			}
			// LIET KE THEO HUYEN
			else if ($dts_xls['TYPE'] == 2 ){
				foreach ($huyens as $huyen):
				$x = 0;
				$datay = $this->Bayden->find('list',
				array('conditions'=>array('Trambvtv.huyen_id' => $huyen['Huyen']['id'] ),'recursive' => 0,'contain' => array('Trambvtv'),
										'fields' => array('Bayden.id')
				) );
	
				$bayden_id = "(";
				foreach ($datay as $dataxx):
				$i = 0;
				$bayden_id = $bayden_id.$dataxx.',';
				$i++;
				endforeach;	
				$bayden_id = $bayden_id.'0)';
				$huyen_id = $huyen['Huyen']['id'];
				$datas["$huyen_id"] = $this->KqBd->find('all',
				array('conditions'=>array('PDtBd.bayden_id IN ' . $bayden_id,
										  'KqBd.doituong_id'=> $dts_xls['DOITUONG'],
										  'YEAR(PDtBd.NGAY_PBD)' => $dts_xls['NAM'],
										  'MONTH(PDtBd.NGAY_PBD)' => $dts_xls['THANG'],
										  ),'recursive' => 0,'contain' => array('PDtBd'),'group' => 'PDtBd.NGAY_PBD',
										  'fields' => array('DAY(PDtBd.NGAY_PBD) as NGAY','SUM(KqBd.SL_TREN_BD) as KQ'),
											
					  )) ;
				$x++;
				endforeach;
				$this->set ('datas',$datas);
				$this->layout = '';
			}
	}
	
	
	
	
	
	
	
	
	
	function index() {
		$this->Ketqua->recursive = 0;
		$this->set('ketquas', $this->paginate());
	}

	function view($id = null) {
		if (!$id) {
			$this->Session->setFlash(__('Invalid Ketqua.', true));
			$this->redirect(array('action'=>'index'));
		}
		$this->set('ketqua', $this->Ketqua->read(null, $id));
	}

	function add() {
		if (!empty($this->data)) {
			$this->Ketqua->create();
			if ($this->Ketqua->save($this->data)) {
				$this->Session->setFlash(__('Thông tin đã được lưu', true));
				$this->redirect(array('action'=>'index'));
			} else {
				$this->Session->setFlash(__('Thông tin chưa được lưu. Bạn vui lòng nhập lại', true));
			}
		}
		$doituongs = $this->Ketqua->Doituong->find('list');
		$phieuDts = $this->Ketqua->PhieuDt->find('list');
		$ndDts = $this->Ketqua->NdDt->find('list');
		$diemDts = $this->Ketqua->DiemDt->find('list');
		$this->set(compact('doituongs', 'phieuDts', 'ndDts', 'diemDts'));
	}

	function edit($id = null) {
		if (!$id && empty($this->data)) {
			$this->Session->setFlash(__('Invalid Ketqua', true));
			$this->redirect(array('action'=>'index'));
		}
		if (!empty($this->data)) {
			if ($this->Ketqua->save($this->data)) {
				$this->Session->setFlash(__('Thông tin đã được lưu', true));
				$this->redirect(array('action'=>'index'));
			} else {
				$this->Session->setFlash(__('Thông tin chưa được lưu. Bạn vui lòng nhập lại.', true));
			}
		}
		if (empty($this->data)) {
			$this->data = $this->Ketqua->read(null, $id);
		}
		$doituongs = $this->Ketqua->Doituong->find('list');
		$phieuDts = $this->Ketqua->PhieuDt->find('list');
		$ndDts = $this->Ketqua->NdDt->find('list');
		$diemDts = $this->Ketqua->DiemDt->find('list');
		$this->set(compact('doituongs','phieuDts','ndDts','diemDts'));
	}

	function delete($id = null) {
		if (!$id) {
			$this->Session->setFlash(__('Invalid id for Ketqua', true));
			$this->redirect(array('action'=>'index'));
		}
		if ($this->Ketqua->del($id)) {
			$this->Session->setFlash(__('Thao tác xóa đã hoàn tất', true));
			$this->redirect(array('action'=>'index'));
		}
	}


	function admin_index() {
		$this->Ketqua->recursive = 0;
		$this->set('ketquas', $this->paginate());
	}

	function admin_view($id = null) {
		if (!$id) {
			$this->Session->setFlash(__('Invalid Ketqua.', true));
			$this->redirect(array('action'=>'index'));
		}
		$this->set('ketqua', $this->Ketqua->read(null, $id));
	}

	function admin_add() {
		if (!empty($this->data)) {
			$this->Ketqua->create();
			if ($this->Ketqua->save($this->data)) {
				$this->Session->setFlash(__('The Ketqua has been saved', true));
				$this->redirect(array('action'=>'index'));
			} else {
				$this->Session->setFlash(__('The Ketqua could not be saved. Please, try again.', true));
			}
		}
		$doituongs = $this->Ketqua->Doituong->find('list');
		$phieuDts = $this->Ketqua->PhieuDt->find('list');
		$ndDts = $this->Ketqua->NdDt->find('list');
		$diemDts = $this->Ketqua->DiemDt->find('list');
		$this->set(compact('doituongs', 'phieuDts', 'ndDts', 'diemDts'));
	}

	function admin_edit($id = null) {
		if (!$id && empty($this->data)) {
			$this->Session->setFlash(__('Invalid Ketqua', true));
			$this->redirect(array('action'=>'index'));
		}
		if (!empty($this->data)) {
			if ($this->Ketqua->save($this->data)) {
				$this->Session->setFlash(__('The Ketqua has been saved', true));
				$this->redirect(array('action'=>'index'));
			} else {
				$this->Session->setFlash(__('The Ketqua could not be saved. Please, try again.', true));
			}
		}
		if (empty($this->data)) {
			$this->data = $this->Ketqua->read(null, $id);
		}
		$doituongs = $this->Ketqua->Doituong->find('list');
		$phieuDts = $this->Ketqua->PhieuDt->find('list');
		$ndDts = $this->Ketqua->NdDt->find('list');
		$diemDts = $this->Ketqua->DiemDt->find('list');
		$this->set(compact('doituongs','phieuDts','ndDts','diemDts'));
	}

	function admin_delete($id = null) {
		if (!$id) {
			$this->Session->setFlash(__('Invalid id for Ketqua', true));
			$this->redirect(array('action'=>'index'));
		}
		if ($this->Ketqua->del($id)) {
			$this->Session->setFlash(__('Ketqua deleted', true));
			$this->redirect(array('action'=>'index'));
		}
	}

}
?>
